/*

Author   : chaos

FileName : subscript_operator.cpp

Date     : 12/08/17 18:18:23

*/
#include <iostream>
#include <cstring>
using namespace std;

class String
{
    private:
        char * string;
        long len;
    public:
        String(const char *);
        void show_string();
        char operator[] (int offset);
};

String::String(const char * i_string)
{
    len = strlen(i_string);
    string = new char [len + 1];
    strcpy(string,i_string);
}

void String::show_string()
{
    cout << "string: " << string << " length :" << len << endl;
}

char String::operator[] (int offset)
{
    if(offset >= len){
        offset = len - 1;
    }
    else if (offset < 0){
        offset = 0;
    }
    return string[offset];
}
int main(int argc ,char *argv[])
{
    String A_String("The C++ Bible");
    A_String.show_string();
    cout << "A_String[4] = " << A_String[4] << endl;
    return 0;
}
